﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.UI;
using UnityEngine.UI;
using XCharts;
using Script.UI.Component;
using LitJson;
using MTUnity.Utils;
using System;

public class UI_Repertory : UIUpdateBase
{
    public Text Txt_TotalRepertory;
    public Text Txt_CurrentReperotoryInBuff;
    public Text Txt_CurrentRepertoryInStore;
    public HistogramGroup Panel_Repertory;
    public Image Img_TotalRepertoryInBuff;
    public Image Img_TotalRepertoryInStory;

    public RectTransform Rect_TotalInBuffTipsNode;
    public RectTransform Rect_TotalInStoreTipsNode;

    public Text Txt_RefreshTime;


    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.Warehouse.Current_Repertory , UpdateCurrentRepertory },
            {JsonFildName.Warehouse.Record_Repertory , UpdateRecordRepertory },
            {JsonFildName.Warehouse.RepertoryRefreshTime , UpdateRefreshTime },
        };
    }
    #endregion

    #region UpdateInterface
    public void UpdateCurrentRepertory(JsonData jsonData)
    {
        if (jsonData.IsObject)
        {
            UpdateCurrentRepertory(
                JsonUtils.ToInt(jsonData[JsonFildName.Warehouse.TotalRepertoryInBuff]) ,
                JsonUtils.ToInt(jsonData[JsonFildName.Warehouse.TotalRepertoryInStore]));
        }

    }

    public void UpdateRecordRepertory(JsonData jsonData)
    {
        if (jsonData.IsArray)
        {
            List<HistogramDataBase> listData = new List<HistogramDataBase>();
            double iMax = 0;
            double min = 0;
            for (int i = 0; i < jsonData.Count; i++)
            {
                HistogramData_Repertory data = new HistogramData_Repertory()
                {
                    Current = (int)jsonData[i][JsonFildName.Warehouse.RepertoryInBuff],
                    Current2 = (int)jsonData[i][JsonFildName.Warehouse.RepertoryInStore],
                    Timestamp = (long)jsonData[i][JsonFildName.Common.Timestamp]
                };

                iMax = Math.Max(iMax, data.Current + data.Current2);
                min = Math.Min(min, data.Current + data.Current2);
                listData.Add(data);
            }
            for (int i = 0; i < listData.Count; i++)
            {
                ((HistogramData_Repertory)listData[i]).Max = iMax;
                ((HistogramData_Repertory)listData[i]).Min = min;

                
            }
            UpdateRecordRepertory(listData);
        }
        
    }
    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    private void UpdateCurrentRepertory(int iTotalRepertoryInBuff , int TotalRepertoryInStore)
    {
        int iTotalRepertory = iTotalRepertoryInBuff + TotalRepertoryInStore;


        Txt_TotalRepertory.text = iTotalRepertory.ToString();
        float fInBuffRadio = 0;
        float fInStoreRadio = 0;
        if (iTotalRepertory != 0)
        {
            fInBuffRadio = (float)iTotalRepertoryInBuff / (float)iTotalRepertory;
            fInStoreRadio = 1 - fInBuffRadio;
        }

       

        Img_TotalRepertoryInBuff.fillAmount = fInBuffRadio;
        Img_TotalRepertoryInStory.fillAmount = fInStoreRadio;



        fInBuffRadio = fInBuffRadio > 0.5f ? 0.5f : 1 - fInBuffRadio / 2;
        fInStoreRadio = fInStoreRadio > 0.5f ? 0.5f : 1 - fInStoreRadio / 2;
        //position

        Vector2 newPosition = new Vector2(Rect_TotalInBuffTipsNode.anchoredPosition.x, Rect_TotalInBuffTipsNode.parent.parent.GetComponent<RectTransform>().sizeDelta.y * fInBuffRadio);
        Rect_TotalInBuffTipsNode.anchoredPosition = newPosition;

        newPosition = new Vector2(Rect_TotalInStoreTipsNode.anchoredPosition.x, Rect_TotalInStoreTipsNode.parent.parent.GetComponent<RectTransform>().sizeDelta.y * fInStoreRadio);
        Rect_TotalInStoreTipsNode.anchoredPosition = newPosition;

        Txt_CurrentReperotoryInBuff.text = iTotalRepertoryInBuff.ToString();
        Txt_CurrentRepertoryInStore.text = TotalRepertoryInStore.ToString();
    }

    private void UpdateRecordRepertory(List<HistogramDataBase> listData)
    {
        Panel_Repertory.Refresh(listData);
    }


    public void SetData(UI_Repertory_Data uiRepertoryData)
    {
        List<HistogramDataBase> listData = new List<HistogramDataBase>();
        for (int i = 0; i < uiRepertoryData.RepertoryRecord.Count; i++)
        {
            HistogramDataBase data = new HistogramData_Repertory()
            {
                Current = uiRepertoryData.RepertoryRecord[i].RepertoryInBuff,
                Current2 = uiRepertoryData.RepertoryRecord[i].RepertoryInStore,
                Timestamp = uiRepertoryData.RepertoryRecord[i].Timestamp
            };
            listData.Add(data);
        }
        Panel_Repertory.Refresh(listData);

        int iTotalRepertory = uiRepertoryData.TotalRepertoryInBuff + uiRepertoryData.TotalRepertoryInStore;


        Txt_TotalRepertory.text = iTotalRepertory.ToString();
        float fInBuffRadio = 0;
        float fInStoreRadio = 0;
        if (iTotalRepertory != 0)
        {
            fInBuffRadio = (float)uiRepertoryData.TotalRepertoryInBuff / (float)iTotalRepertory;
            fInStoreRadio = 1 - fInBuffRadio;
        }

        fInBuffRadio = 0.75f;
        fInStoreRadio = 0.25f;

        Img_TotalRepertoryInBuff.fillAmount = fInBuffRadio;
        Img_TotalRepertoryInStory.fillAmount = fInStoreRadio;

        

        fInBuffRadio = fInBuffRadio > 0.5f ? 0.5f : 1 - fInBuffRadio / 2;
        fInStoreRadio = fInStoreRadio > 0.5f ? 0.5f : 1 -  fInStoreRadio / 2;
        //position

        Vector2 newPosition = new Vector2(Rect_TotalInBuffTipsNode.anchoredPosition.x, Rect_TotalInBuffTipsNode.parent.parent.GetComponent<RectTransform>().sizeDelta.y * fInBuffRadio);
        Rect_TotalInBuffTipsNode.anchoredPosition = newPosition;

        newPosition = new Vector2(Rect_TotalInStoreTipsNode.anchoredPosition.x, Rect_TotalInStoreTipsNode.parent.parent.GetComponent<RectTransform>().sizeDelta.y * fInStoreRadio);
        Rect_TotalInStoreTipsNode.anchoredPosition = newPosition;
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}
